/*
 * Copyright 2009-2010 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.filesystem;

import java.io.File;
import java.util.Arrays;

import javax.swing.Icon;

import org.kommando.core.catalog.CatalogObject;
import org.kommando.core.catalog.SimpleCatalogObject;
import org.kommando.core.util.Assert;
import org.kommando.filesystem.core.DiskObject;
import org.kommando.filesystem.core.FileObject;
import org.kommando.filesystem.core.FileSystemObject;
import org.kommando.filesystem.core.FolderObject;

/**
 * Factory for creating {@link CatalogObject} from a {@link File}.
 * 
 * @author Peter De Bruycker
 */
public class FileSystemCatalogObjectFactory {

    // TODO create a test case

    private FileSystemCatalogObjectFactory() {
        // only static methods
    }

    /**
     * Create a {@link CatalogObject} from a {@link File}.
     * 
     * @param file
     *            the {@link File}
     * @return the {@link CatalogObject} representing the {@link File}
     */
    public static CatalogObject create(File file) {
        Assert.argumentNotNull("file", file);

        FileSystemObject fileSystemObject = createFileObject(file);

        Icon icon = fileSystemObject.getIcon();
        String name = fileSystemObject.getName();
        String description = fileSystemObject.getDescription();

        SimpleCatalogObject fileSystemCatalogObject = new SimpleCatalogObject("file:" + file.getAbsolutePath(), name, description, icon);
        fileSystemCatalogObject.setExtension(FileSystemObject.class, fileSystemObject);

        return fileSystemCatalogObject;
    }

    /**
     * Create a {@link FileSystemObject} (either a {@link DiskObject}, {@link FileObject} or a {@link FolderObject}) from a {@link File}.
     * 
     * @param file
     *            the {@link File}
     * @return the {@link FileSystemObject} representing the {@link File}
     */
    public static FileSystemObject createFileObject(File file) {
        Assert.argumentNotNull("file", file);
        Assert.isTrue("file " + file + " doesn't exist", file.exists());

        if (Arrays.asList(File.listRoots()).contains(file)) {
            return new DiskObject(file);
        }

        if (file.isFile()) {
            return new FileObject(file);
        }

        if (file.isDirectory()) {
            return new FolderObject(file);
        }

        throw new IllegalArgumentException("Unable to create a FileSystemObject for " + file);
    }

}
